﻿@using paycircuit.com.google.iap;
@*
 * Copyright (C) 2011, 2012, 2013 Paycircuit.com, Thinking Spot, Ed Chavez
 *  http://goo.gl/jty8r
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *@
@functions {

    //*** I M P O R T A N T ***
    //use your credentials
    //This is a simple sample, you can define these in web.config (or _AppStart.cshtml options) as needed
    //When copying/pasting make sure you don't include spaces
    string MY_SELLER_ID = "YOUR_SELLER_ID";
    string MY_SELLER_SECRET = "YOUR_SELLER_SECRET";


    /***** I M P O R T A N T ***
     * This sample DOES NOT INCLUDE required code for you to confirm/verify payment
     * You must add code to check/verify successful paymenst/handling of your callback handler which is processed asynchronously (separate from this web browser/client process).
     */

    string theJWT()
    {
        try
        {
            JWTHeaderObject HeaderObj = new JWTHeaderObject(JWTHeaderObject.JWTHash.HS256, "1", "JWT");
            InAppItemRequestObject request = new InAppItemRequestObject()
            {
                name = "Burp",
                description = "A digital representation of food for your palete",
                price = "1.99",
                currencyCode = RequestObject.SupportedCurrencies.USD.ToString()
            };
            InAppItemObject ClaimObj = new InAppItemObject(request) { iss = MY_SELLER_ID };
            return JWTHelpers.buildJWT(HeaderObj, ClaimObj, MY_SELLER_SECRET);
        }
        catch
        {
            return string.Empty;
        }
    }

    //Example using _AppStart.cshtml to store your credentials - and yes, subscriptions
    //See _AppStart.cshtml
    string theJWTv2()
    {
        try
        {
            JWTHeaderObject HeaderObj = new JWTHeaderObject(JWTHeaderObject.JWTHash.HS256, "1", "JWT");

            InAppItemSubscriptionRequestObject request = new InAppItemSubscriptionRequestObject()
            {
                name = "Monthly Burp",
                description = "A digital representation of food for your palete delivered every month!",
                initialPayment = new InAppSubscriptionInitialPaymentObject()
                {
                    price = "1.99",
                    currencyCode = RequestObject.SupportedCurrencies.USD.ToString(),
                    paymentType = "prorated"
                },
                recurrence = new InAppSubscriptionRecurrenceObject()
                {
                    frequency = "monthly",
                    price = "0.99",
                    currencyCode = RequestObject.SupportedCurrencies.USD.ToString(),
                    recurrenceStartDate = DateTime.Now.AddMonths(1),  //start recurrence a month from today
                    numRecurrences = "12"
                }
            };
            InAppItemObject ClaimObj = new InAppItemObject(request) { iss = @AppState["MY_SELLER_ID"].ToString() };

            return JWTHelpers.buildJWT(HeaderObj, ClaimObj, @AppState["MY_SELLER_SECRET"].ToString());
        }
        catch
        {
            return string.Empty;
        }
    }
}
@{
    Page.Title = "Google Wallet for Digital Goods";
    Layout = "~/shared/_layout.cshtml";
}
@section head {
    <meta name="description" content="A simple Google Wallet for Digital Goods demo page" />
}

@section lateboundScripts{
    <script src="https://sandbox.google.com/checkout/inapp/lib/buy.js"></script>
    <script type="text/javascript">

        //Deprecated - do not use!!!
        //google.load('payments', '1.0', {
        //    'packages': ['sandbox_config']

        //});

        function purchase(callback) {
            google.payments.inapp.buy({
                "parameters": {},
                "jwt": "@Html.Raw(theJWT())",
                "success": function (result) {
                    if (typeof callback === "function") {
                        callback(true, result);
                    }
                },
                "failure": function (result) {
                    if (typeof callback === "function") {
                        callback(false, result);
                    }
                }
            }
        )
        };

        function purchase2(callback) {
            google.payments.inapp.buy({
                "parameters": {},
                "jwt": "@Html.Raw(theJWTv2())",
                "success": function (result) {
                    if (typeof callback === "function") {
                        callback(true, result);
                    }
                },
                "failure": function (result) {
                    if (typeof callback === "function") {
                        callback(false, result);
                    }
                }
            }
        )
        };

        /*** S A M P L E   O N L Y ****
        *******************************
        !You should verify server side!
        *******************************                
        */
        var sampleParseResult = function (isgood, data) {
            var _console = (typeof window.console === "undefined");
            if (isgood) {
                var _str = "Verify Order No. " + data.response.orderId;
                _str += "\nDetails:\n";
                _str += data.request.name + " " + data.request.description + "\n";
                (data.request.price) ? _str += data.request.price + "\n" : _str += "Subscription payment: " + data.request.initialPayment.price + " monthly: " + data.request.recurrence.price + " for " + data.request.recurrence.numRecurrences + " months\n";
                alert(_str);
                if (!_console) {
                    console.log(data);
                }
            } else {
                alert("failed");
                if (!_console) {
                    console.log(data);
                }
            }
        };

    </script>
}
<div class="container_12" style="margin-top: 20px;">
    <div itemscope itemtype="http://schema.org/Product">
        <header>
            <div class="grid_12 header">
                <h1 itemprop="name">Google Wallet For Digital Goods Demo (.Net)</h1>
                <p itemprop="description">This is a running demo of a <a href="https://code.google.com/p/google-in-app-payments-dotnet-sample/" itemprop="url">.Net library for Google Wallet for Digital Goods API</a>.</p>
            </div>
        </header>
        <div class="grid_6">
            <ul>
                <li>One Time Burp - US$1.99</li>
                <li>A digital representation of food for your palete</li>
            </ul>
            <input id="foo" type="button" onclick="purchase(sampleParseResult);" value="Burp!"
                style="padding: 5px;" />
        </div>
        <div class="grid_6">
            <ul>
                <li>Monthly Burp Subscription (save $1.00 each month!)</li>
                <li>12 month subscription to a digital representation of food for your palete</li>
            </ul>
            <input id="bar" type="button" onclick="purchase2(sampleParseResult);" value="Subscribe!"
                style="padding: 5px;" />
        </div>
    </div>
</div>
